﻿//// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");

//1.Hello
//using System;
//namespace Program
//{
//    class Hello
//    {
//        static void Main()
//        {
//            Console.WriteLine("你好！我是周爽！");
//        }
//    }
//}

//2.素数输出
using System;
namespace Program
{
    class Hello
    {
        static void Main()
        {
            int low = 0, high = 0;
            bool is_valid_data = false;
            //WriteLine()打印完自动换行，Write()不会换行
            Console.WriteLine("请输入运算上下限范围(非负整数)：");
            while (!is_valid_data)
            {
                Console.Write("请输入下限：");
                string low_input = Console.ReadLine();
                //检查是不是整数
                if (int.TryParse(low_input, out low))//接受string，输出int--low
                {
                    if (low < 0)
                    {
                        Console.WriteLine("请输入非负整数。");
                        is_valid_data = false;
                    }
                    else is_valid_data = true;
                }
            }
            //上限判断
            is_valid_data = false;
            while (!is_valid_data)
            {
                Console.Write("请输入上限：");
                string high_input = Console.ReadLine();
                //check int
                if (int.TryParse(high_input, out high))
                {
                    if (high < 0)
                    {
                        Console.WriteLine("请输入非负整数。");
                        is_valid_data = false;
                    }
                    else if (high < low)
                    {
                        Console.Write("请注意上限必须大于下限！");
                        is_valid_data = false;
                    }
                    else is_valid_data = true;
                }
            }
            Console.WriteLine("上下限之间的素数是：");
            int count = 0;
            for (int num = low; num <= high; num++)
            {
                if (isPrime(num))
                {
                    Console.Write(num + " ");
                    count++;
                    if (count % 10 == 0) Console.WriteLine();//10个换行
                }
            }
            if (count % 10 != 0) Console.WriteLine();
            static bool isPrime(int num)
            {
                if (num <= 1) return false;
                if (num == 2) return true;
                if (num % 2 == 0) return false;
                for (int i = 3; i <= Math.Sqrt(num); i +=2)
                {
                    if (num % i == 0) return false;
                }
                return true;
            }
        }
    }
}